define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'district'], function ($, undefined, Backend, Table, Form, District) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/shop/index' + location.search,
                    //add_url: 'shop/shop/add',
                    edit_url: 'shop/shop/edit',
                    del_url: 'shop/shop/del',
                    multi_url: 'shop/shop/multi',
                    //import_url: 'shop/shop/import',
                    table: 'shop',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='user.nickname']", form).addClass("selectpage")
                    .data("source", "user/user/index")
                    .data("primaryKey", "nickname")
                    .data("field", "nickname")
                    .data("orderBy", "id asc");
                $("input[name='province_id']", form).addClass("selectpage")
                    .data("source", "district/index")
                    .data("primaryKey", "id")
                    .data("field", "shop_name")
                    .data("orderBy", "id asc");
                $("input[name='city_id']", form).addClass("selectpage")
                    .data("source", "district/index")
                    .data("primaryKey", "id")
                    .data("field", "shop_name")
                    .data("orderBy", "id asc");
                $("input[name='area_id']", form).addClass("selectpage")
                    .data("source", "district/index")
                    .data("primaryKey", "id")
                    .data("field", "shop_name")
                    .data("orderBy", "id asc");
                $("input[name='circle_id']", form).addClass("selectpage")
                    .data("source", "circle/index")
                    .data("primaryKey", "id")
                    .data("field", "name")
                    .data("orderBy", "id asc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'is_show desc, is_top desc, weigh desc, id desc',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        //{field: 'store_id', title: __('Store_id')},
                        //{field: 'circle_id', title: __('Circle_id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'user.nickname', title: __('User_id'), operate: 'LIKE'},
                        {field: 'logo', title: __('Logo'), formatter: Table.api.formatter.image, operate: false},
                        {field: 'flag', title: __('Flag'), operate: false, formatter: Table.api.formatter.flag},
                        {field: 'province_id', title: __('Province_id'), visible: false},
                        {field: 'province_name', title: __('Province_id'), operate: false},
                        {field: 'city_id', title: __('City_id'), visible: false},
                        {field: 'city_name', title: __('City_id'), operate: false},
                        {field: 'area_id', title: __('Area_id'), visible: false},
                        {field: 'area_name', title: __('Area_id'), operate: false},
                        {field: 'circle_id', title: __('Circle_id'), visible: false},
                        {field: 'circle_name', title: __('Circle_id'), operate: false},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'vip_tage', title: __('Vip_tage'), operate:'BETWEEN',
                            //formatter: function(value, row, index){
                            //    return (value/10) + "折";
                            //}
                        },
                        {field: 'avg_price', title: __('Acg_price')+"/元", operate:'BETWEEN'},
                        {field: 'clock_number', title: __('Clock_number')},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'is_show', title: __('Is_show'),formatter: Table.api.formatter.toggle, searchList:{"0":'下架', "1":"上架"}},
                        {field: 'is_top', title: __('Is_top'),formatter: Table.api.formatter.toggle, searchList:{"0":'不推荐', "1":"推荐"}},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $("input[name='province_id']").data('params', function(){
                return {custom:{parent_id:1}};
            });

            var province_id = $("input[name='province_id']").val();
            //province_id = 27833;
            $("input[name='city_id']").data('params', function(){
                if(!province_id){
                    province_id = -1;
                }
                return {custom:{parent_id:province_id}};
            });

            var city_id = $("input[name='city_id']").val();
            //city_id = 28141;
            $("input[name='area_id']").data('params', function(){
                if(!city_id){
                    city_id = -1;
                }
                return {custom:{parent_id:city_id}};
            });

            var area_id = $("input[name='area_id']").val();
            $("input[name='circle_id']").data('params', function(){
                return {custom:{area_id:area_id}};
            });

            $(document).on('change', "input[name='province_id']", function(){
                province_id = $(this).val();
                $("input[name='city_id']").selectPageClear();
                $("input[name='area_id']").selectPageClear();
            });

            $(document).on('change', "input[name='city_id']", function(){
                city_id = $(this).val();
                $("input[name='area_id']").selectPageClear();
            });

            $(document).on('change', "input[name='area_id']", function(){
                area_id = $(this).val();
                $("input[name='circle_id']").selectPageClear();
            })
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                let area_id = $('.district select.district').data('id');
                //console.log(area_id);
                $("input[name='row[circle_id]']").data('params', function(){
                    return {custom:{area_id: area_id}};
                });
                $(document).on('change', '.district select.district', function(){
                    area_id = $(this).val();
                    $("input[name='row[circle_id]']").selectPageClear();
                });
                District.districtCpcdsEvent({type:2});

                $(document).on('input', '#c-vip_tage', function(){
                    let vip_tage = $(this).val();
                    if(vip_tage > 10){
                        $(this).val(10);
                        return true;
                    }else if(vip_tage < 0){
                        $(this).val(0);
                        return true;
                    }
                    vip_tage = vip_tage.replace(/[^\d.]/g, '');
                    vip_tage = vip_tage.replace(/\.{2,}/g, '.');
                    vip_tage = vip_tage.replace('.', '$#$').replace(/\./g, '').replace('$#$', '.');
                    vip_tage = vip_tage.replace(/^(\-)*(\d+)\.(\d\d).*/, '$1$2.$3');
                    vip_tage = vip_tage.replace(/^\./, '0.');
                    //if (vip_tage.indexOf(".") < 0 && vip_tage != "") vip_tage = parseFloat(vip_tage);
                    $(this).val(vip_tage);
                });
                $(document).on('input', '#c-avg_price', function(){
                    let avg_price = $(this).val();
                    if(avg_price > 999999){
                        $(this).val(999999);
                    }else if(avg_price < 0){
                        $(this).val(0);
                    }
                });
            }
        }
    };
    return Controller;
});